/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.converters;

import cz.muni.fi.pa165.machineryrental.dto.MachineDTO;
import cz.muni.fi.pa165.machineryrental.dto.RevisionDTO;
import cz.muni.fi.pa165.machineryrental.entities.Revision;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Marian Lorenc, 374122
 */
public class RevisionDTOConverter {

    /**
     * Converts Revision entity to RevisionDTO
     *
     * @param entity
     * @return RevisionDTO which is set by Revision entity
     */
    public static RevisionDTO toDTO(Revision entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        RevisionDTO dto = new RevisionDTO();
        return applyToDTO(dto, entity);
    }

    /**
     * Converts RevisionDTO to Revision entity
     *
     * @param dto
     * @return Revision entity which is set by RevisionDTO
     */
    public static Revision toEntity(RevisionDTO dto) {
        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        Revision entity = new Revision();
        entity.setId(dto.getId());
        entity.setDateOfRevision(dto.getDateOfRevision());
        entity.setDescription(dto.getDescription());
		
		if (dto.getMachine() != null) {
			entity.setMachine(MachineDTOConverter.toEntity(dto.getMachine()));
		}

        return entity;
    }

    /**
     * Overwrites DTO with information in entity
     */
    public static RevisionDTO applyToDTO(RevisionDTO dto, Revision entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        dto.setId(entity.getId());
        dto.setDateOfRevision(entity.getDateOfRevision());
        dto.setDescription(entity.getDescription());
		
		if (entity.getMachine() != null) {
			
			MachineDTO machineDTO = MachineDTOConverter.toDTO(entity.getMachine());
			dto.setMachine(machineDTO);
		}

        return dto;
    }

    /**
     * Converts list of rental entities to list of revision DTOs.
     *
     * @param revisions Revisions to convert.
     * @return List of DTOs according to given revisions.
     */
    public static List<RevisionDTO> toDTOList(List<Revision> revisions) {
        if (revisions == null || revisions.isEmpty()) {
            return new ArrayList<RevisionDTO>();
        }

        List<RevisionDTO> result = new ArrayList<RevisionDTO>();
        for (Revision r : revisions) {
            result.add(RevisionDTOConverter.toDTO(r));
        }
        return result;
    }
}
